package com.qiaoyanqing.ximalaya.downloadmodule.download_fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.downloadmodule.download_fragment.adapter.DownLoad_Hot_Adapter;
import com.qiaoyanqing.ximalaya.downloadmodule.download_fragment.bean.DownLoadModule_Hot_Entity;
import com.qiaoyanqing.ximalaya.downloadmodule.download_fragment.bean.Hot_list;
import com.qiaoyanqing.ximalaya.downloadmodule.download_fragment.callback.DownLoad_Hot_HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotFragment extends Fragment implements Callback<DownLoadModule_Hot_Entity> {
    private RecyclerView recyclerView;
    private DownLoad_Hot_HttpService service;
    private List<Hot_list>list;
    private DownLoad_Hot_Adapter adapter;

    public HotFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_hot, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.hot_recycleview);
        LinearLayoutManager  manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(manager);
        service = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(DownLoad_Hot_HttpService.class);
        service.getData().enqueue(this);
//        list = new ArrayList<>();
//        adapter = new DownLoad_Hot_Adapter(getContext(),list);
//        recyclerView.setAdapter(adapter);

    }

    @Override
    public void onResponse(Call<DownLoadModule_Hot_Entity> call, Response<DownLoadModule_Hot_Entity> response) {
        Log.d("3333",""+response.body().getList());
        list = new ArrayList<>();
        list= response.body().getList();
        adapter = new DownLoad_Hot_Adapter(getContext(),list);
        recyclerView.setAdapter(adapter);
//        adapter.notifyDataSetChanged();

    }

    @Override

    public void onFailure(Call<DownLoadModule_Hot_Entity> call, Throwable t) {

    }
}
